package org.demo.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisTestJob {

    private static Logger logger = LoggerFactory.getLogger(RedisTestJob.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(fixedRate = 1000)
    public void job1() {
        logger.info("job1 start");
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        try {
            valueOperations.set("TEST", "TEST", 10, TimeUnit.SECONDS);
        } catch (Exception e) {
            logger.error("redis value operate failed", e);
        }
    }

}
